package Final;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import javax.imageio.ImageIO;


public class Matriz {
	private int fil, col;


	static final int MAXFIL = 40;
	static final int MINFIL = 5;
	
	static final int MAXCOL = 40;
	static final int MINCOL = 5;
	
	public Elemento matriz[][];

    public Matriz() {
            setFil(MAXFIL);
            setCol(MAXCOL);
            matriz = new Elemento [fil][col];
            for (int i = 0; i < fil; i++)
            	for (int j = 0; j < col; j++) 
            		matriz[i][j] = new Elemento();
    }

    public void setFil(int _fil){ 
    	fil = _fil; 
    }
    public void setCol(int _col){ 
    	col = _col; 
    }
    public void setFilCol(int _fil, int _col){
    	setFil(_fil); setCol(_col); 
    }
    
    public int getFil(){ 
    	return fil; 
    }
    public int getCol(){ 
    	return col; 
    }
    

    public boolean cargarMatriz(String namefile){
    	boolean result = true;
        int i = 0;
        int j = 0;
        try {
        	String buffer;
        	FileReader Fin;
        	BufferedReader BufferIn;
            Fin = new FileReader(new File(namefile));
            BufferIn = new BufferedReader(Fin);
            BufferedImage image0 = ImageIO.read(new File("fondo.jpg"));
            BufferedImage image1 = ImageIO.read(new File("Robot.jpg"));
            BufferedImage image2 = ImageIO.read(new File("Obstaculo.jpg"));
            if((buffer = BufferIn.readLine()) != null){
                int aux=0;
                StringTokenizer stText= new StringTokenizer(buffer);
                while (stText.hasMoreTokens()) {
                    String ele = stText.nextToken();
                    if(aux == 0)
                        setFil(Integer.parseInt(ele));
                    else
                        setCol(Integer.parseInt(ele));
                    aux++;
                }
            }
	        while((buffer = BufferIn.readLine()) != null){
	            StringTokenizer stText= new StringTokenizer(buffer);
	            while (stText.hasMoreTokens()) {
	                String ele = stText.nextToken();
	                this.matriz[i][j].setVisitada(false);
	                    if(ele.equals(Integer.toString(Elemento.ROBOT))){
	                        this.matriz[i][j].setNumImage(Elemento.ROBOT);
	                        this.matriz[i][j].setImage(image1);
	                        this.matriz[i][j].setEstado(false);
	                        Panel.removeVectorElement("Robot");
	                    }else if(ele.equals(Integer.toString(Elemento.OBSTACULO))){
	                        this.matriz[i][j].setNumImage(Elemento.OBSTACULO);
	                        this.matriz[i][j].setImage(image2);
	                        this.matriz[i][j].setEstado(false);
	                        Panel.removeVectorElement("OBSTACULO");
	                    }else{
	                        this.matriz[i][j].setNumImage(Elemento.FONDO);
	                        this.matriz[i][j].setImage(image0);
	                        this.matriz[i][j].setEstado(true);
	                    }
	                    j++;
	                    
	            }
	            i++;
	            j=0;
	        }
                                 
	    } catch (Exception e) {
	        e.printStackTrace();
	        result = false;
	    }
	    return result;
}


public boolean guardarMatriz(String namefile){
		
        boolean result = true;
        try {
        	PrintWriter BufferOut;
        	FileWriter Fout;
	        Fout = new FileWriter(new File(namefile));
	        BufferOut = new PrintWriter(Fout);
	        BufferOut.println(Integer.toString(this.getFil()) + " " + Integer.toString(this.getCol()));
	        for (int i = 0; i < fil; i++){
	        	for (int j = 0; j < col; j++){
	        		if(matriz[i][j].getNumImage() == Elemento.ROBOT)
		                 BufferOut.print(Integer.toString(Elemento.ROBOT));
		             else if(matriz[i][j].getNumImage() == Elemento.OBSTACULO)
	                	 BufferOut.print(Integer.toString(Elemento.OBSTACULO));
		             else
		            	 BufferOut.print(Integer.toString(Elemento.FONDO));
		         if(j != col - 1)
		        	 BufferOut.print(" ");
	        	}
	        	if(i != fil - 1)
	        		BufferOut.print("\n");
		   }
	   BufferOut.close();
	                
       } catch (Exception e) {
    	   e.printStackTrace();
    	   result = false;
       }
	   return result;
	}
}